package com.zwh.chatgpt.test.application.job;


import com.zwh.chatgpt.test.domain.ai.IOpenAI;
import com.zwh.chatgpt.test.domain.zsxq.IZsxqAi;
import com.zwh.chatgpt.test.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import com.zwh.chatgpt.test.domain.zsxq.model.vo.Topics;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

//问答任务
@EnableScheduling
@Configuration
public class ChatgptSchedule {
    private Logger logger = LoggerFactory.getLogger(ChatgptSchedule.class);//添加日记

    @Value("${chatgpt-ai.groupId}")//我真的服了，记得这里加的是大括号
    private  String groupId;

    @Value("${chatgpt-ai.cookie}")
    private  String cookie;

    @Resource
    private IZsxqAi iZsxqAi;

    @Resource
    private IOpenAI openAI;

    @Scheduled(cron = "0/5 * * * * ?")//轮巡时间
    public void run (){

        try {

            //检索问题
            UnAnsweredQuestionsAggregates unAnsweredQuestionsAggregates = iZsxqAi.queryUnAnsweredQuestionsTopicId(groupId, cookie);
            List<Topics> topics = unAnsweredQuestionsAggregates.getResp_data().getTopics();
            Topics topic = topics.get(0);//直接检索第一条问题
            String answer = openAI.doDouBao(topic.getTalk().getText().trim());//这就是调用豆包回答问题的结果


            logger.info("topicID: {}",topic.getTopic_id());
            logger.info("豆包返回的内容：{}", answer);//到这里answer已经接收到了豆包回答问题的内容

            //把结果传回知识新球
            iZsxqAi.answer(groupId,cookie,topic.getTopic_id(),answer,false );


        }catch (Exception e){
            logger.error("自动回答问题异常",e);
        }
    }

}
